<template>
  <div>
    <div>
      <el-button type="primary" @click="onAddProd">添加公告</el-button>
    </div>
    <div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="prodId" label="ID" />
        <el-table-column prop="prodTitle" label="公告标题" />
        <el-table-column prop="typeName" label="公告类型" />
        <el-table-column prop="statusName" label="状态" />
        <el-table-column prop="ressage" label="内容" />
      </el-table>
    </div>
    <div style="margin-top: 20px">
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加公告" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="公告标题"
            :label-width="formLabelWidth"
            prop="prodTitle"
          >
            <el-input v-model="form.prodTitle" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="公告类型"
            :label-width="formLabelWidth"
            prop="prodType"
          >
            <el-select v-model="form.prodType" placeholder="请选择">
              <el-option label="通知" value="1" />
              <el-option label="不通知" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="公告状态" :label-width="formLabelWidth">
            <el-radio-group v-model="form.prodStatus">
              <el-radio value="1">正常</el-radio>
              <el-radio value="2">关闭</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="内容" :label-width="formLabelWidth">
            <el-input v-model="form.ressage" autocomplete="off" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="onAddProdList(ruleFormRef)">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getProdList, saveProdList } from "@/api/api";
import { ElMessage } from "element-plus";
const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
});

//校验
import type { FormInstance, FormRules } from "element-plus";
const ruleFormRef = ref<FormInstance>();
const rules = ref<FormRules>({
  prodTitle: [{ required: true, message: "不能为空", trigger: "blur" }],
  prodType: [{ required: true, message: "不能为空", trigger: "blur" }],
});

//添加对话框
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  prodId: null,
  prodStatus: null,
  prodTitle: "",
  prodType: null,
  ressage: "",
  statusName: "",
  typeName: "",
});

const onAddProd = () => {
  dialogFormVisible.value = true;
};

const onAddProdList = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      saveProdList(form.value).then((res: any) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const handleSizeChange = (val: number) => {
  searchForm.value.pageSize = val;
  loadList();
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  searchForm.value.pageNum = val;
  loadList();
  console.log(`current page: ${val}`);
};

const loadList = () => {
  getProdList(searchForm.value).then((res: any) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadList();
});
</script>

<style scoped>
</style>